import { createRouter, createWebHashHistory } from 'vue-router'
import storeInstance from "@/pinia"
import { useTitle } from "@vueuse/core"

export const constantRouter = [
    {
        path: '/',
        name: 'Index',
        meta: {
            title: '首页',
            onCache: false,
            onTag: false
        },
        component: () => import('@/views/Index/Index')
    },
    {
        path: '/:tag/:operationId*',
        name: 'ApiInfo',
        meta: {
            title: '接口文档',
            onCache: true,
            onTag: true
        },
        component: () => import('@/views/ApiInfo/ApiInfo')
    },
    {
        path: '/commonResponse',
        name: 'CommonResponse',
        meta: {
            title: '通用响应参数',
            onCache: true,
            onTag: true
        },
        component: () => import('@/views/ApiInfo/CommonResponse')
    },
    {
        path: '/model',
        name: 'Model',
        meta: {
            title: '模型组件',
            onCache: true,
            onTag: true
        },
        component: () => import('@/views/ApiInfo/Model')
    },
    {
        path: '/:catchAll(.*)',
        meta: {
            onCache: false,
            onTag: false
        },
        component: { template: '<div></div>' }
    }
]

const router = createRouter({
    history: createWebHashHistory(import.meta.env.BASE_URL),
    routes: constantRouter
})

router.beforeEach(async (to, _from, next) => {
    useTitle("Api Documentation")
    // 判断用户是否登录，未登录跳转到首页
    if (storeInstance.mainStore.getMenu && !storeInstance.mainStore.getMenu.length) {
        const status = await storeInstance.mainStore.setApiInfo()
        if (status) {
            return next({ ...to, replace: true })
        } else if (to.path !== '/') {
            replaceState()
            return next({ path: "/" })
        }
    }
    next()
})


router.beforeEach(() => {

})

router.beforeResolve(() => {
})

export default router
